package cn.hh.wechatkit.api.card;

import cn.hh.wechatkit.data.postdata.card.metaCardGetInfoPostData;
import cn.hh.wechatkit.data.returndata.card.Meta_RData_MeetingCardInfo;
import cn.hh.wechatkit.exception.Wx_Exception_BadNetwork;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidJsonParse;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidResponse;
import cn.hhchat.kit.http.httpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static cn.hh.wechatkit.helper.WxConst.GetCardInfo_POST_URL;


/**
 * Created this one by HMH on 2017/5/9.
 */
@Slf4j
public class WxAPI_Card_GetCardInfo_Api {
    private final static Logger logger = LoggerFactory.getLogger(WxAPI_Card_GetCardInfo_Api.class);

    /**
     * 获取会议卡券的信息
     * @param CardId 卡券id
     * @param ACCESS_TOKEN 基础令牌
     * @return 卡券信息
     * @throws IOException io异常
     * @throws Wx_Exception_InvalidResponse 响应异常
     */
    public static Meta_RData_MeetingCardInfo getInfo(String CardId, String ACCESS_TOKEN)  {
        String url = GetCardInfo_POST_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);
        metaCardGetInfoPostData postData = new metaCardGetInfoPostData();
        postData.setCard_id(CardId);
        Response response = null;
        try {
            response = httpUtil.postJson(url, postData);
        } catch (IOException e) {
            log.error(" ==> 网络请求失败", e);
            throw new Wx_Exception_BadNetwork("网络请求失败", e);
        }
        //获取数据
        if (response != null) {
            String body;
            try {
                body = response.body().string();
                return JSON.parseObject(body, Meta_RData_MeetingCardInfo.class);
            } catch (IOException e) {
                log.error(" ==> json转换失败", e);
                throw new Wx_Exception_InvalidJsonParse("json转换失败");
            }
        }
        throw new Wx_Exception_InvalidResponse("没有收到响应，或者请求出错");
    }
}
